(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:ConfessionCtrl
     * @description
     * # ConfessionCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('ConfessionCtrl', ConfessionCtrl);

    ConfessionCtrl.$inject = ['$scope', 'dataService', 'helper', '$rootScope', '$location'];

    function ConfessionCtrl($scope, dataService, helper, $rootScope, $location) {
        //表白活动列表
        $scope.listAction = function() {
            $scope.pageSize = 10;
            $scope.getList(1);

            $scope.$on('event:up-store-id', function() {
                $scope.store_id = $rootScope.store_id
                $scope.getList(1);
            });
        }

        $scope.getList = function(pageNo) {
            var query = {
                "content": {
                    page_no: pageNo ? pageNo : "1",
                    page_size: $scope.pageSize,
                    store_id: $rootScope.store_id ? $rootScope.store_id : $scope.store_id
                }
            };

            dataService.confessionGet(query).then(function(list) {
                if (list) {
                    for (var i in list.confession_list) {
                        list.confession_list[i].diff_time = $scope.diff_time(list.confession_list[i].start_time, list.confession_list[i].end_time)
                    }
                    $scope.confession_list = list.confession_list;
                    $scope.total_count = list.total_count;
                }
            });
        }

        //js计算两个时间的时间差
        $scope.diff_time = function(start_time, end_time) {
            var s_time = new Date().getTime();
            var start = new Date(start_time).getTime();
            var end = new Date(end_time).getTime();

            //判断当前时间是否大于结束时间
            if (s_time > end) {
                return 0;
            }

            //判断当前时间是否大于开始时间
            if (s_time > start && s_time < end) {
                var time = new Date().getTime();
            } else {
                var time = new Date(start_time).getTime();
            }

            var diff = end - time;

            //计算出相差天数  
            var days = Math.floor(diff / (24 * 3600 * 1000));

            //计算出小时数  
            var leave1 = diff % (24 * 3600 * 1000); //计算天数后剩余的毫秒数  
            var hours = Math.floor(leave1 / (3600 * 1000));
            //计算相差分钟数  
            var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数  
            var minutes = Math.floor(leave2 / (60 * 1000));

            //计算相差秒数  
            var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数  
            var seconds = Math.round(leave3 / 1000);

            var returnStr = seconds + "秒";
            if (minutes > 0) {
                returnStr = minutes + "分" + returnStr;
            }
            if (hours > 0) {
                returnStr = hours + "小时" + returnStr;
            }
            if (days > 0) {
                returnStr = days + "天" + returnStr;
            }
            return returnStr;
        }

        //表白活动添加
        $scope.addAction = function() {
            var confession_activity_id = $location.search().confession_activity_id;

            if (confession_activity_id) {
                var query = { "content": { "store_id": $rootScope.store_id, "confession_activity_id": confession_activity_id } };

                dataService.confessionGet(query).then(function(result) {
                    if (result.total_count) {
                        $scope.confession_data = result.confession_list[0];
                        $scope.confession_data.position1 = $scope.confession_data.position;
                        $scope.confession_data.position = JSON.stringify($scope.confession_data.position);
                        $scope.confession_activity_id = confession_activity_id;
                    }
                });
            } else {
                var data = {
                    "store_id": $rootScope.store_id, //门店id
                    "name": "站在桥上看风景", //活动名称
                    "start_time": $scope.getDateStr(0), //活动开始时间
                    "end_time": $scope.getDateStr(30), //活动结束时间
                    "background_img": [],
                    "confession_copy": [],
                };
                $scope.confession_data = data;
            }
        }

        //增加文案
        $scope.addCopy = function() {
            $scope.confession_data.confession_copy.push({ "title": "" });
        }

        //增加图片
        $scope.addImg = function() {
            $scope.confession_data.background_img.push({ "img": "" });
        }

        //删除表白文案
        $scope.removeTitle = function(index) {
            $scope.confession_data.confession_copy.splice(index, 1);
        }

        //删除背景图片
        $scope.removeBgImg = function(index) {
            $scope.confession_data.background_img.splice(index, 1);
        }

        //表白活动
        $scope.confessionAdd = function() {
            if (!$scope.confession_data.name) {
                helper.myAlert("error:表白标题不能为空！");
                return false;
            }

            if ($scope.confession_data.end_time < $scope.confession_data.start_time) {
                helper.myAlert("error:开始时间不能大于结束时间");
                return false;
            }

            //校验表白文案
            if (!$scope.confession_data.confession_copy.length) {
                helper.myAlert("error：表白文案不能为空！");
                return false;
            } else {
                for (var i in $scope.confession_data.confession_copy) {
                    if (!$scope.confession_data.confession_copy[i].title) {
                        helper.myAlert("error：表白文案不能为空！");
                        return false;
                    }
                }
            }

            //校验背景图片
            if (!$scope.confession_data.background_img.length) {
                helper.myAlert("error：背景图片不能为空！");
                return false;
            } else {
                for (var i in $scope.confession_data.background_img) {
                    if (!$scope.confession_data.background_img[i].img) {
                        helper.myAlert("error：背景图片不能为空不能为空！");
                        return false;
                    }
                }
            }
            // console.error($scope.confession_data);
            // return false;
            var confession_activity_id = $location.search().confession_activity_id;
            if (confession_activity_id) {
                dataService.confessionUpdate($scope.confession_data).then(function(result) {
                    if (result.Msg == "ok") {
                        $scope.broadcast();
                        $scope.choseBroadcast();
                    }
                });
            } else {
                if ($scope.confession_activity_id) {
                    $scope.confession_data.confession_activity_id = $scope.confession_activity_id;

                    dataService.confessionUpdate($scope.confession_data).then(function(result) {
                        if (result.confession_activity_id) {
                            $scope.broadcast();
                            $scope.choseBroadcast();
                        }
                    });
                } else {
                    $scope.oneAdd();
                }
            }
        }

        $scope.oneAdd = function() {
            dataService.confessionAdd($scope.confession_data).then(function(result) {
                if (result.Msg == "ok") {
                    $scope.confession_activity_id = result.confession_activity_id;
                    $scope.choseBroadcast();
                } else {
                    helper.myAlert("error：" + result.Msg);
                    return false;
                }
            });
        }

        $scope.showPosition = function() {
            var className = $("#position").attr('class');

            if (className.indexOf('zhide') > -1) {
                $("#position").removeClass('zhide');
            } else {
                $("#position").addClass('zhide');
            }
        }

        //已选择表白商品保存
        $scope.chooseItemAdd = function(confession_activity_id) {
            if ($scope.chosedItems) {
                dataService.confessionItemAdd($scope.confession_activity_id, $scope.chosedItems).then(function(result) {
                    if (result.Msg == "ok") {
                        helper.myAlert("success", "#/confession/list");
                    } else {
                        helper.myAlert("error：" + result.Msg);
                        return false;
                    }
                });
            } else {
                if ($scope.confession_item_list.length) {
                    helper.myAlert("success", "#/confession/list");
                } else {
                    helper.myAlert("error：没有选择商品，无法保存");
                    return false;
                }
            }
        }

        //已选择的满就送商品
        $scope.itemdata = function(items) {
            // console.error("items-->", items, items.length);
            $scope.chosedItems = items;
        }

        $scope.broadcast = function() {
            $scope.confessionItemGet(1);
            $scope.confessionChoosedItemGet();

            $rootScope.$broadcast("event:choose-item");
        }

        $scope.choseBroadcast = function() {
            $rootScope.$broadcast("event:choose-item");
        }

        $scope.confessionChoosedItemGet = function() {
            var query = {
                "content": {
                    "checked_item": "checked",
                    "confession_activity_id": $scope.confession_activity_id
                }
            };

            dataService.confessionItemGet(query).then(function(list) {
                if (list) {
                    $scope.confession_item_list = list.confession_item_list;
                    $scope.total_count = list.total_count;
                }
            });
        }

        $scope.confessionItemGet = function(pageNo) {
            var query = {
                "content": {
                    page_no: pageNo ? pageNo : "1",
                    page_size: $scope.pageSize,
                    "confession_activity_id": $scope.confession_activity_id
                }
            };

            dataService.confessionItemGet(query).then(function(list) {
                if (list) {
                    $scope.item_list = list.confession_item_list;
                    $scope.itemsTotal = list.total_count;
                }
            });
        }

        $scope.confessionItemDelete = function(id, confession_activity_id) {
            helper.myConfirm('确认删除吗？', function() {
                if (id && confession_activity_id) {
                    dataService.confessionItemDelete(id, confession_activity_id).then(function(result) {
                        if (result.Msg == "ok") {
                            $scope.confessionItemGet(1);
                        } else {
                            helper.myAlert("error：" + result.Msg);
                            return false;
                        }
                    });
                } else {
                    helper.myAlert("error：参数不正确，无法删除！");
                    return false;
                }
            });
        }

        //tab切换
        $scope.tabCut = function($event) {
            $event.preventDefault();
            $($event.currentTarget).tab('show');
        }

        //切换优惠类型
        $scope.changeType = function(flag) {
            $scope.mjs_data.type_id = flag;
        }

        //设置优惠方式
        $scope.setMode = function(mode) {
            $scope.mjs_data.mode = mode;
        }

        //活动时间计算
        $scope.getDateStr = function(addDayCount) {
            var dd = new Date();
            dd.setDate(dd.getDate() + addDayCount);
            var y = dd.getFullYear();
            var m = (dd.getMonth() + 1) < 10 ? "0" + (dd.getMonth() + 1) : (dd.getMonth() + 1);
            var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
            var h = dd.getHours();
            var i = dd.getMinutes();
            if (i < 10) {
                i = "0" + i;
            }
            var s = dd.getSeconds();
            if (s < 10) {
                s = "0" + s;
            }

            return y + "-" + m + "-" + d + " " + h + ":" + i + ":" + s;
        }

        //详情列表
        $scope.detailAction = function() {
            $scope.pageSize = 10;
            $scope.confession_activity_id = $location.search().confession_activity_id;
            if ($scope.confession_activity_id) {
                $scope.getDetailList(1);

                $scope.$on('event:up-store-id', function() {
                    $scope.store_id = $rootScope.store_id
                    $scope.getDetailList(1);
                });
            } else {
                helper.myAlert("error：表白活动编号错误！");
                return false;
            }
        }

        $scope.getDetailList = function(pageNo) {
            var query = {
                "content": {
                    page_no: pageNo ? pageNo : "1",
                    page_size: $scope.pageSize,
                    confession_activity_id: $scope.confession_activity_id,
                    store_id: $rootScope.store_id ? $rootScope.store_id : $scope.store_id
                }
            };

            dataService.confessionDetailGet(query).then(function(list) {
                // return false;
                if (list) {
                    $scope.detail_list = list.detail_list;
                    $scope.detail_count = list.detail_count;
                }
            });
        }
    }
})();